package com.badibadi.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.badibadi.activity.BaseActivity;
import com.badibadi.activity.UpPictures2Activity;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.ImageLoader;
import com.badibadi.mytools.ImageTool;
import com.badibadi.uniclubber.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class UpPictures2Adapter extends BaseAdapter {
	/**
	 * 最大上传图片数量
	 */
	final int UpPictureMax = 8;
	public final int SCREENW = 0;
	public final int SCREENH = 1;
	float picWidth;
	Context context;
	public final String TAG = "aaa";
	String dangqianlujing;
	/**
	 * 报存有dangqianlujing下的所有jif，jpg，png
	 */
	ArrayList<String> FileName;
	/**
	 * 记录选中项
	 */
	boolean[] itemClick;
	/**
	 * 记录选中项的地址，可以直接提交的数据
	 */
	Map<Integer, String> mapData;

	ImageLoader imageLoader;
	LayoutInflater layoutInflater;
	ImageTool imageTool;
	DisplayUtil displayUtil;

	public UpPictures2Adapter(Context ct, String dangqianlujing,
			ArrayList<String> FileName) {
		context = ct;
		// dangqianlujing=FilePath;
		// File file=new File(FilePath);
		// picName=file.list();
		this.dangqianlujing = dangqianlujing;
		this.FileName = FileName;
//		   
		itemClick = new boolean[FileName.size()];
		for (int i = 0; i < FileName.size(); i++) {
			itemClick[i] = false;
		}

		picWidth = ((getScreenSize(SCREENW)) / 3);
		imageLoader = ImageLoader.getInstance();
		imageTool = ImageTool.getInstance();
		displayUtil = new DisplayUtil();

		mapData = new HashMap<Integer, String>();
//		   
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return FileName.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return (dangqianlujing + "/" + FileName.get(position));
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		layoutInflater = LayoutInflater.from(context);
		convertView = layoutInflater.inflate(
				R.layout.uppictures2_import_layout, null);

		ImageView img = (ImageView) convertView
				.findViewById(R.id.Up_pictures2_import_img);
		// img.setI
		// fl.addView(img);
		ImageView gou = (ImageView) convertView
				.findViewById(R.id.Up_pictures2_import_gou);

		if (itemClick[position] == true) {
			gou.setVisibility(View.VISIBLE);
		} else {
			gou.setVisibility(View.INVISIBLE);
		}
		try {
			asyncImageLoad(img, gou,
					dangqianlujing + "/" + FileName.get(position));
//			   
//					"up2===========>" + dangqianlujing + "/"
//							+ FileName.get(position));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return convertView;
	}

	/**
	 * 重要
	 * 
	 * @param imageView
	 * @param path
	 */
	private void asyncImageLoad(ImageView img, ImageView gou, String filePath) {
		AsyncImageTask asyncImageTask = new AsyncImageTask(img, gou);
		asyncImageTask.execute(filePath);
	}

	/**
	 * 重要
	 * 
	 * @author Administrator 第一个String传入doInBackground 第二个是进度
	 *         第三个是doInBackground返回给onPostExecute
	 */
	private final class AsyncImageTask extends AsyncTask<String, Integer, Bitmap> {
		private ImageView img;
		private ImageView gou;

		public AsyncImageTask(ImageView img, ImageView gou) {
			// TODO Auto-generated constructor stub
			this.img = img;
			this.gou = gou;
//			   
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			// 子线程中执行
			// TODO Auto-generated method stub
//			   
			String pathName = params[0];
			Bitmap bmp = ImageLoader.decodeSampledBitmapFromResource(pathName,
					(int) picWidth);

			//    
			if (bmp != null) {
				Bitmap bmp2 = ImageTool.createBitmapBySize(bmp, bmp.getWidth(),
						bmp.getWidth());
//				   
				return bmp2;
			}

			return bmp;

		}

		@Override
		protected void onPostExecute(Bitmap result) {// 运行在主线程
			// TODO Auto-generated method stub

			if (img != null && result != null) {
				img.setImageBitmap(result);
			}
		}
	}

	/**
	 * 
	 * @param RETURN_SCREEN
	 *            RETURN_SCREENW为返回宽 RETURN_ SCREENH 为返回高
	 * @return 返回-1为空
	 */
	public float getScreenSize(int RETURN_SCREEN) {
		DisplayMetrics metrics = new DisplayMetrics();// 获取屏幕宽高
		((UpPictures2Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);// 1。获取窗口管理器，2默认的屏幕，3获取屏幕的大小（赋值给metrcs）

		float ScreenW = metrics.widthPixels;
		float ScreenH = metrics.heightPixels;
		if (RETURN_SCREEN == SCREENW)
			return ScreenW;
		else if (RETURN_SCREEN == SCREENH)
			return ScreenH;

		return -1;
	}

	public boolean getItemClick(int position) {
		return itemClick[position];
	}

	public boolean[] getItemClick() {
		return itemClick;
	}

	public void setItemClick(boolean[] itemClick) {
		this.itemClick = itemClick;
	}

	/**
	 * 重要：关系到在这里更新指定项目（这里面的操作包括： 存储数据到map中，给定限制，只能选择八张图片） 更新界面
	 * 
	 * @param view
	 * @param position
	 */
	public void ChangeItemClick(View view, int position) {

		if (this.itemClick[position] == true) {
			ImageView iv = (ImageView) view
					.findViewById(R.id.Up_pictures2_import_gou);
			iv.setVisibility(View.INVISIBLE);
			;

			itemClick[position] = false;
			if (mapData.containsKey(position))
				;
			mapData.remove(position);
			// updateView

		} else if (this.itemClick[position] == false) {
			if (mapData.size() >= UpPictureMax) {
				Toast.makeText(context, 
						context.getResources().getString(R.string.l_xb4) + UpPictureMax + "张", 0)
						.show();
			} else {
				itemClick[position] = true;
				ImageView iv = (ImageView) view
						.findViewById(R.id.Up_pictures2_import_gou);
				iv.setVisibility(View.VISIBLE);
				if (mapData.containsKey(position) == false)
					;
				mapData.put(position, (String) getItem(position));
//				   
//						+ (String) getItem(position));
			}
		}
	}

}